#include <stdio.h>
#define N 1010
int m[N][2*N];
char a[N];
int main()
{
	int i, j, n, q, s, l, k, r, c, A;
	long long t;
	for(scanf("%d%d", &n, &q), i=0; i<n; i++)
		for(scanf("%s", a), j=-i; j<=i; m[i][j+N]=m[i][j+N-1]+a[j+i]-'0', j++);
	for(t=0, A=1; q--; A=((long long)A*1234567+7654321)%1000000007)
	{
		r=A%n+1;
		c=A%(2*r-1)+1;
		k=A%(n-r+1)+1;
		j=c-r+N; i=r-1; l=k;
		for(s=0; l--; s+=m[i+l][j+l]-m[i+l][j-l-1]);
		t+=s;
	}
	printf("%lld\n", t);
	return 0;
}